net http
using System.Collections.Generic;
using System.Collections;
using System;
//using System.Net.Http;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class HttpRestful : MonoBehaviour
{
private static HttpRestful _instance;
public static HttpRestful Instance
{
get
{
if(_instance == null)
{
GameObject goRestful = new GameObject("HttpRestful");
_instance = goRestful.AddComponent<HttpRestful>();
}
return _instance;
}
}
#region Get请求
/// <summary>
/// Get请求
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
public void Get(string url, Action<bool, string> actionResult = null)
{
StartCoroutine(_Get(url, actionResult));
}
private IEnumerator _Get(string url, Action<bool, string> action)
{
using (UnityWebRequest request = UnityWebRequest.Get(url))
{
yield return request.SendWebRequest();
string resstr = "";
if (request.isNetworkError || request.isHttpError)
{
resstr = request.error;
}
else
{
resstr = request.downloadHandler.text;
}
if (action != null)
{
action(request.isHttpError, resstr);
}
}
}
#endregion
#region POST请求
public void Post(string url, string data, Action<bool, string> actionResult = null)
{
StartCoroutine(_Post(url, data, actionResult));
}
private IEnumerator _Post(string url, string data, Action<bool, string> action)
{
using (UnityWebRequest request = new UnityWebRequest(url, "POST"))
{
request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(data));
request.SetRequestHeader("content-type", "application/json;charset=utf-8");
request.downloadHandler = new DownloadHandlerBuffer();
yield return request.SendWebRequest();
string resstr = "";
if (request.isNetworkError || request.isHttpError)
{
resstr = request.error;
}
else
{
resstr = request.downloadHandler.text;
}
if (action != null)
{
action(request.isHttpError, resstr);
}
}
}
#endregion
}
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.Networking;
using System.Linq;
using System.Text;
using UnityEngine.UI;
public class Netbutton : MonoBehaviour
{
// [Header("按钮")]
// public Button btn;
[Space]
[Header("显示")]
public Text txtshow;
public void ClickBtn() {
print("ClickBtn by code");
//StartCoroutine(GetJson());
InitAction();
string url = "https://api.myip.la/cn?json";
HttpRestful.Instance.Get(url, actionRes);
}
//定义一个返回数据的Action
public Action<bool, string> actionRes;
private void Awake() {
Button btn = this.GetComponent<Button>();
btn.onClick.AddListener(ClickBtn);
// UpdateButton btn = this.GetComponent<Button>();
//btn.interactable = true;
// btn.transition = Selectable.Transition.None;
// Image img = this.GetComponent<Image>();
}
// Start is called before the first frame update
void Start()
{
print("init button");
}
// Update is called once per frame
void Update()
{
}
DaraItem r;
private void InitAction()
{
actionRes = new Action<bool, string>((bl, str) =>
{
if (bl)
{
print("error : " + str);
}
else
{
//string resjson = "{\"array\":" + "str" + "}";
print("ok1");
r = JsonUtility.FromJson<DaraItem>(str);
print(r.ip);
string json = JsonUtility.ToJson(r, true);
print(json);
}
});
}
// Start is called before the first frame update
public IEnumerator GetJson()
{
print("GetJson");
var url = "https://api.myip.la/cn?json";
var www = UnityWebRequest.Get(url);
www.SetRequestHeader("Content-Type", "application/json");
yield return www.SendWebRequest();
if (www.isHttpError || www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
print(www.downloadHandler.text + "lllll");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class DaraItem
{
/// <summary>
///
/// </summary>
public string ip;
/// <summary>
///
/// </summary>
public LocalItem location;
public List<string> cd;
}
[Serializable]
public class LocalItem {
public string city;
public string country_code;
public string country_name;
public string latitude;
public string longitude;
public string province;
}